package org.wj.config.temperature;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

import java.io.IOException;


public class SecondarySortMapper extends MapReduceBase implements Mapper<LongWritable, Text, DateTemperaturePair, LongWritable> {

    @Override
    public void map(LongWritable key, Text value, OutputCollector<DateTemperaturePair, LongWritable> output, Reporter reporter) throws IOException {
        // key为当前行的起始偏移量
        String[] split = value.toString().split(",");
        DateTemperaturePair pair = new DateTemperaturePair();
        pair.setDay(new Text(split[2]));
        pair.setYearMonth(new Text(String.format("%s-%s", split[0], split[1])));
        pair.setTemperature(new LongWritable(Integer.parseInt(split[3])));
        output.collect(pair, pair.getTemperature());
    }
}
